

/**
 * Created by user on 2017/10/9.
 */
public class LinkedStack<T> implements Stack<T>
{
    private int count;
    private LinearNode<T> top = new LinearNode<T>(null);//表头,数据为空
    private LinearNode<T> end = new LinearNode<T>(null);//表尾

    //----------------------------------------------------------------
    //  Creates an empty stack using the default capacity.
    // ----------------------------------------------------------------
    public LinkedStack() {
        top.setNext(end);
        count=0;
    }
//----------------------------------------------------------------
//  Removes the element at the top of this stack and returns a
//  reference to it. Throws an EmptyCollectionException if the
//  stack contains no elements.
// ----------------------------------------------------------------
public T pop() throws EmptyCollectionException {
        if (count == 0) throw new EmptyCollectionException ("Pop operation failed. " + "The stack is empty.");
    T result = top.getNext().getElement();
    top.setNext(top.getNext().getNext());
    top.setElement(null);

    count--;
return result;
}
//----------------------------------------------------------------
    //   Returns a string representation of this stack.
// ----------------------------------------------------------------
    public String toString() {
        String result = "<top of stack>\n";
        LinearNode current = top;
        while (current != null) {
            result += current.getElement() + "\n";
            current = current.getNext();
        }
        return result + "<bottom of stack>";
    }
    //----------------------------------------------------------------
//  The following methods are left as programming projects.
// ----------------------------------------------------------------

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public void push(T element) {
            LinearNode<T> now = new LinearNode<T>(element);//now是用来保存加入元素的节点类的实例
            now.setNext(top.getNext());//指向top指向的对象
            top.setNext(now);
            count++;
    }

    @Override
    public T peek() {
        return top.getNext().getElement();
    }

    @Override
    public boolean isEmpty() {
        if (count==0)
            return true;
        else
            return false;
    }

    @Override
    public int size() {
        return count;
    }

    public static void main(String[] args) {
        LinkedStack mLinked = new LinkedStack();
        mLinked.push(1);
        System.out.println(mLinked.isEmpty());
    }


}

//************************************************************
//  LinearNode.java       Java Foundations
// //  Represents a node in a linked list.
// ************************************************************
       class LinearNode<T> {
        private LinearNode<T> next;
        private T element;
//----------------------------------------------------------------
//  Creates an empty node.
// ----------------------------------------------------------------
public LinearNode() {
        next = null;
        element = null; }
//----------------------------------------------------------------
//  Creates a node storing the specified element.
// ---------------------------------------------------------------
public LinearNode (T elem) {
        next = null;
        element = elem; }
//----------------------------------------------------------------
//  Returns the node that follows this one.
// ----------------------------------------------------------------
public LinearNode<T> getNext() {
        return next;
    }
//----------------------------------------------------------------
//  Sets the node that follows this one.
// ----------------------------------------------------------------
public void setNext (LinearNode<T> node) {
        next = node;
    }
//----------------------------------------------------------------
//  Returns the element stored in this node.
// ----------------------------------------------------------------
public T getElement() {
        return element;
    }
public void setElement (T elem) {
        element = elem;
    }

        }

